package com.fafi.selfmvc.validator;

import com.fafi.selfmvc.vo.MyBean;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public class MyBeanValidator implements Validator {
	@Override
	public boolean supports(Class<?> clazz) {
		return MyBean.class.equals(clazz);
	}

	@Override
	public void validate(Object target, Errors errors) {
		ValidationUtils.rejectIfEmpty(errors, "name", "name.empty");
		MyBean p = (MyBean) target;
		if (p.getAge() < 0) {
			errors.rejectValue("age", "negative value","年龄不能为负值");
		} else if (p.getAge() > 110) {
			errors.rejectValue("age", "too old","年龄不能超过110");
		}
	}
}
